import React from 'react';
import config from "../config/config";
import { message, Modal } from 'antd';
import * as moduleService from '../services/moduleService';

export default {
  namespace: "moduleModel",
  state: {
    collapsed: false,
    activeHeadMenuKey: "home",
    activeSideMenuKey: "platform",
    menuData: null,
    pageUrl: null,
    pathUrlList: [],
    paneTabs: [config.frame_menu.sider.home[0]],
    openedSubMenuKey: null,
    homeView: null,
    tokenModel: null,
    codeModel: null,
    pageLoading: false,
    themeDrawerVisible: false,
    themeColor: config.DEFAULT_THEME_COLOR,
    siderColor: config.DEFAULT_SIDER_COLOR,
    themeStyle: "sideMenu",
    selectedStyle: "theme",
    isBlockStyle: true,
    timeoutModalCount: 0,
    userInfoVisible: false,
    systemStatus: "login",
  },
  reducers: {
    updateState(state, { payload }) {
      return { ...state, ...payload }
    }
  },
  effects: {
    *init({ payload: params }, { select, call, put }) {
      yield put({ type: "getAppMenu", payload: {} });
      yield put({ type: "refreshPage", payload: {} });
    },

    *getAppMenu({ payload: params }, { select, call, put }) {
      const res = yield call(moduleService.getAppMenu, params);
      if (res.code == "200") {
        console.log("getAppMenu menuData ===> " + res.data);
        sessionStorage.menuData = JSON.stringify(res.data);
        yield put({ type: "updateState", payload: { menuData: res.data }});
      } else {
        console.log("config menuData ===> " + res.data);
        const menuList = yield call(moduleService.getAppMenuFromConfig);
        let menuData = {};
        menuData["list"] = menuList;
        menuData = {...menuData, ...config.frame_menu};
        sessionStorage.menuData = JSON.stringify(menuData);
        yield put({ type: "updateState", payload: { menuData }});
      }
    },

    *logout({ payload: params }, { select, call, put }) {
      yield put({ type: "updateState", payload: { pageLoading: true }});
      const { userName, password } = window._USERINFO_ ? JSON.parse(window._USERINFO_) : JSON.parse(sessionStorage.userInfo);
      const res = yield call(moduleService.logout, { userName, password });
      if (res.code == "200") {
        console.log("homeModel logout res => ", res);
      }
      yield put({ type: "updateState", payload: { pageLoading: false, paneTabs: [], activeHeadMenuKey: "home", activeSideMenuKey: null, systemStatus: "logout" }});
    },

    *addActiveRoute({ payload: params }, { select, call, put }) {
      let { menuData, paneTabs } = yield select(state => state.moduleModel);
      const {activeHeadMenuKey, paneTabList } = yield call(moduleService.getActivedMenu, params, paneTabs, menuData.sider);
      console.log("open tab, activeHeadMenuKey ===>>> " + activeHeadMenuKey + ", activeSideMenuKey ===>>> " + params.key);
      yield put({ type: "updateState", payload: { paneTabs: paneTabList, activeHeadMenuKey, activeSideMenuKey: params.key }});
    },

    *refreshPage({payload: params}, {select, call, put}) {
      const tokenModel = {};
      const token = sessionStorage.getItem("token");
      window._TOKEN_ = token;
      const userInfo = sessionStorage.getItem("userInfo");
      window._USERINFO_ = userInfo;
      tokenModel["token"] = token;
      tokenModel["userInfo"] = userInfo;
      let menuData = !!sessionStorage.getItem("menuData") ? JSON.parse(sessionStorage.getItem("menuData")) : null;
      // 兼容单独运行前端服务
      if (!menuData) {
        const menuList = moduleService.getAppMenuFromConfig();
        menuData = {};
        menuData["list"] = menuList;
        menuData = {...menuData, ...config.frame_menu};
      }
      yield put({type: "updateState", payload: { tokenModel, menuData, systemStatus: sessionStorage.getItem("systemStatus") }});
    },
  },
  subscriptions: {
    onListenIFrameMessage({ dispatch, history }) {
      window.addEventListener("message", function (e) {
        // 监听页面超时事件，确定后直接跳转到登陆界面
        console.log("moduleModel Listener e => ", e);
        if (!!e && !!e.data && !(e.data instanceof Object)) {
          // kunlun-system-web请求打开菜单页面的监听
          const message = !!e && !!e.data && JSON.parse(e.data) || {};
          message.isAddRoute && dispatch({type: "addActiveRoute", payload: message});
        }
      });
    },
    setup({ dispatch, history }) {
      const pathname = window.location.pathname;
      console.log("moduleModel subscriptions setup pathname => ", pathname);
      if (pathname == "/kunlun") {
        dispatch({ type: "init", payload: {} });
      }
    },
  },
};
